package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsOrderGoodsMapper;
import com.qf.mapper.DtsOrderMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsOrder;
import com.qf.pojo.DtsOrderGoods;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsOrderService;
import com.qf.vo.DayStatis;
import com.qf.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class DtsOrderServiceImpl implements DtsOrderService {
    @Autowired
    DtsOrderMapper dtsOrderMapper;

    @Autowired
    StatMapper statMapper;
    @Override
    public Integer findCount() {
        Integer count = dtsOrderMapper.selectCount(null);
        return count;
    }

    @Override
    public List<DayStatis> findOrderAtms() {

        return statMapper.findOrderAtms();
    }

    @Override
    public List<OrderVo> findOrder() {

        return statMapper.findOrder();
    }

    @Override
    public IPage<DtsOrder> findByPage(Integer page, Integer limit, String sort, String order, Integer orderStatusArray, Integer userId, Integer orderSn) {
        IPage<DtsOrder> iPage = new Page<>(page,limit);
        QueryWrapper<DtsOrder> qw = new QueryWrapper<>();
        if(orderStatusArray!=null){
            qw.eq("order_status",orderStatusArray);
        }
        if(userId!=null){
            qw.eq("user_id",userId);
        }
        if(orderSn!=null){
            qw.eq("order_sn",orderSn);
        }

        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }

        dtsOrderMapper.selectPage(iPage,qw);



        return iPage;
    }

    @Override
    public List<Map<String,Object>> findlistShipChannel() {
//        List<DtsOrder> dtsOrders = dtsOrderMapper.selectList(null);
//        List<CatVo1> catVo1s = dtsOrders.stream().map(dtsOrder -> {
//            CatVo1 catVo = new CatVo1();
//            catVo.setValue(dtsOrder.getShipChannel());
//            catVo.setLabel("");
            List<Map<String,Object>> data = new ArrayList<>();
            Map<String,Object> map1= new HashMap<>();
            map1.put("value","HTKY");
            map1.put("label","百世快递");
            data.add(map1);
            Map<String,Object> map2= new HashMap<>();
            map2.put("value","ZTO");
            map2.put("label","中通快递");
            data.add(map2);
            Map<String,Object> map3= new HashMap<>();
            map3.put("value","YTO");
            map3.put("label","圆通速递");
            data.add(map3);
            Map<String,Object> map4= new HashMap<>();
            map4.put("value","YD");
            map4.put("label","韵达速递");
            data.add(map4);
//            return catVo;
//        }).collect(Collectors.toList());

        return data;
    }
    @Autowired
    DtsOrderGoodsMapper orderGoodsMapper;

    @Autowired
    DtsUserMapper userMapper;

    @Autowired
    DtsOrderMapper orderMapper;
    @Override
    public Map<String, Object> finddetial(Integer id) {


        DtsOrder dtsOrder = orderMapper.selectById(id);
        DtsUser dtsUser = userMapper.selectById(dtsOrder.getUserId());

        LambdaQueryWrapper<DtsOrderGoods> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(DtsOrderGoods::getOrderId,id);
        List<DtsOrderGoods> dtsOrderGoods = orderGoodsMapper.selectList(qw1);

        Map<String,Object> map = new HashMap<>();
        map.put("orderGoods",dtsOrderGoods);
        map.put("user",dtsUser);
        map.put("order",dtsOrder);
        return map;
    }

    @Override
    public void ship(Integer orderId, String shipChannel, String shipSn) {
        DtsOrder order = new DtsOrder();
        order.setOrderStatus(301);
        order.setUpdateTime(new Date());
        order.setShipChannel(shipChannel);
        order.setShipSn(shipSn);
        LambdaQueryWrapper<DtsOrder> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsOrder::getId,orderId);
        dtsOrderMapper.update(order,qw);

    }
}
